using Microsoft.AspNetCore.Mvc;
using WebapiTemplate.Domain.Entity;
using WebapiTemplate.Application.Common.Interface;
using Microsoft.AspNetCore.Authorization;
using WebapiTemplate.Application.RequestDto;
using WebapiTemplate.Application.ResponesDto;

namespace WebapiTemplate.Api.Controllers; //net sdk6.0的命名空间风格

[ApiController]
[Route("[controller]")]
[Authorize] //开启该控制器全局权限认证
public class UserController : ControllerBase
{
    //定义用户表实例字段
    private readonly IRepository<User> _user;

    //定义操作token的IdentityService实例字段
    private readonly IIdentityService _identityService;

    //依赖注入得到字段的真正实例
    public UserController(IRepository<User> user, IIdentityService identityService)
    {
        _user = user;
        _identityService = identityService;
    }

    //测试Vue的登入
    // [HttpPost("vuelogin")]
    // [AllowAnonymous]
    // public async Task<dynamic> VueLogin(CreateUserDto createUserDto)
    // {
    //     return "Vue登入";
    // }


    //注册用户
    [HttpPost("register")]
    [AllowAnonymous]
    public async Task<dynamic> CreateUser(CreateUserDto createUserDto)
    {
        if (createUserDto.Username != "admin")
        {
            await _user.AddAsync(
                new User
                {
                    Username = createUserDto.Username,
                    Password = createUserDto.Password,
                    AvatarId = createUserDto.AvatarId
                }
            );
            //上传图片
            return new
            {
                code = 1000,
                data = _user.Table.FirstOrDefault(x => x.Username.Equals(createUserDto.Username)),
                msg = "注册成功"
            };
        }
        else
        {
            return new { code = 400, data = "", msg = "注册失败" };
        }
    }

    //获取用户数据列表
    [HttpGet("userlist")]
    public dynamic UserList()
    {
        var userlist = _user.Table.ToList();

        return userlist;
    }

    //编辑
    [HttpPut("updateuser")]
    public async Task<dynamic> UpdateUser(CreateUserDto createUserDto)
    {
        var updateuser = _user.Table.FirstOrDefault(x => x.Id == createUserDto.Id);

        //只有关联头像AvatarId不更改
        updateuser.Username = createUserDto.Username;
        updateuser.Password = createUserDto.Password;
        if(createUserDto.AvatarId!=new Guid()){
            updateuser.AvatarId = createUserDto.AvatarId;
        }
        
        // updateuser.DisplayOrder = createUserDto.DisplayOrder;
        updateuser.Remarks = createUserDto.Remarks;

        return await _user.UpdateAsync(updateuser);
    }

    //删除
    [HttpDelete("deleteuser/{Id}")]
    public async Task<dynamic> DeleteUser(Guid Id)
    {
        await _user.DeleteByIdAsync(Id);

        return "ok";
    }

    //查询
    [HttpGet("queryuser")]
    public dynamic QueryUser(string keyword)
    {
        //这里手动一个(不对，应该是半自动把，因为还是用了实现类的Table^.^)
        //这里查询只要匹配就可以，返回集合类型
        var queryuser = _user.Table
            .Where(x => x.Username.Equals(keyword) || x.Password.Equals(keyword))
            .ToList();

            return queryuser;
    }

    //用户第一次登入获取token
    [HttpPost]
    [Route("login")]
    [AllowAnonymous] //跳过权限验证
    public async Task<dynamic> Login(CreateUserDto createUserDto)
    {
        var appToken = await _identityService.ValidateUserAsync(createUserDto);

        return new
        {
            user = _user.Table.FirstOrDefault(x => x.Username.Equals(createUserDto.Username)),
            token = appToken
        };
    }

    //用户再次登入验证token
    [HttpPost("againlogin")]
    public dynamic AgainLogin(CreateUserDto createUserDto)
    {
        return "登入成功！";
    }

    //刷新token
    [HttpPost("refreshtoken")]
    [AllowAnonymous] //跳过权限验证
    public async Task<dynamic> Refreshtoken(AppTokenDto appTokenDto)
    {
        var refresh_appToken = await _identityService.RefreshTokenAsync(appTokenDto);
        return refresh_appToken;
    }
}
